package cc.rengu.redp.bizimpl.upmp.service.impl;

import cc.rengu.redp.bizimpl.upmp.domain.*;
import cc.rengu.redp.bizimpl.upmp.dao.BDeviceActionMapper;
import cc.rengu.redp.bizimpl.upmp.service.IBDeviceActionService;
import cc.rengu.redp.bizimpl.upmp.service.IUpmpBMchntBaseInfoService;
import cc.rengu.redp.bizimpl.upmp.service.IUpmpSysParamService;
import cc.rengu.redp.bizimpl.upmp.vo.UpmpUserInfoVO;
import cc.rengu.redp.bizimpl.utils.CommonUtils;
import cc.rengu.redp.bizimpl.utils.DateUtils;
import cc.rengu.redp.common.domain.QueryRequest;
import cc.rengu.redp.common.utils.SortUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * @author lizk
 */
@Service
@DS("upmp")
public class BDeviceActionServiceImpl extends ServiceImpl<BDeviceActionMapper, BDeviceAction> implements IBDeviceActionService {

    @Autowired
    private IUpmpBMchntBaseInfoService bMchntBaseInfoService;
    @Autowired
    private IUpmpSysParamService upmpSysParamService;

    @Override
    public BDeviceAction queryDeviceAction(String deviceNo, String action) {
        LambdaQueryWrapper<BDeviceAction> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BDeviceAction::getDeviceNo, deviceNo).eq(BDeviceAction::getAction, action);
        List<BDeviceAction> list = this.list(queryWrapper);
        if (list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public IPage<BDeviceAction> findByPage(QueryRequest queryRequest, BDeviceAction bDeviceAction) {
        try {
            LambdaQueryWrapper<BDeviceAction> queryWrapper = new LambdaQueryWrapper<>();
            if (!StringUtils.isEmpty(bDeviceAction.getInstId())) {
                queryWrapper.eq(BDeviceAction::getInstId, bDeviceAction.getInstId());
            }
            if (!StringUtils.isEmpty(bDeviceAction.getDeviceNo())) {
                queryWrapper.and(i -> i.like(BDeviceAction::getDeviceNo, bDeviceAction.getDeviceNo())
                        .or().like(BDeviceAction::getBrandName, bDeviceAction.getBrandName())
                        .or().like(BDeviceAction::getModelName, bDeviceAction.getModelName()));
            }

            Page<BDeviceAction> page = new Page<>();

            SortUtil.handlePageSort(queryRequest, page, true);

            //设置模型数量和商户名称
            Page<BDeviceAction> infoPage = this.page(page, queryWrapper);
            for (BDeviceAction deviceAction : infoPage.getRecords()) {
                String mchntNo = deviceAction.getMchntNo();

                if (!StringUtils.isEmpty(mchntNo)) {
                    UpmpBMchntBaseInfo byMchntNo = bMchntBaseInfoService.findByMchntNo(mchntNo);
                    deviceAction.setMchntName(byMchntNo.getMchntName());
                }
            }
            return infoPage;
        } catch (Exception e) {
            log.error("获取语音设备信息失败", e);
            return null;
        }
    }

    @Override
    public void updateDeviceAction(BDeviceAction bDeviceAction) {
        //获取当前登录人信息
        UpmpUserInfoVO user = CommonUtils.getCurrentUser(upmpSysParamService);
        String lastUpddt = DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS");
        bDeviceAction.setLastUpdateTime(lastUpddt);
        bDeviceAction.setLastOprId(user.getId());
        this.updateById(bDeviceAction);

    }
}
